package cn.jdy.anquan.admin;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.anquan.entity.CompanySafe;
import cn.jdy.anquan.entity.Survey;
import cn.jdy.anquan.service.SurveyService;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.HelpInfo;

@Controller
@RequestMapping("/survey")
public class SurveyCtrl extends BaseCtrl {
  @Autowired
  private SurveyService surveyService;
//默认列表路径
  private static String viewName = "redirect:/survey/list?s=";
  
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  @ResponseBody
  public ModelAndView save(Survey survey){
    try {
      if(survey.getId()==null){
    	survey.setCreateTime(DateUtil.getCurrentDate2());
        survey.setIp(request.getRemoteAddr());
        surveyService.save(survey);
      }else{
        surveyService.update(survey);
      }
      
      
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "投票失败", e);
    }
    return new ModelAndView("redirect:/survey/list");
  }
  
  @RequestMapping(value = "/look", method = RequestMethod.GET)
  public ModelAndView query(Survey survey){
    ModelAndView mv = new ModelAndView("/help/lookGusebook.jsp");
    List<Survey> list = surveyService.query();
    int count = surveyService.getCount();
    mv.addObject("count", count);
    
    String scale = "0%";
    Survey sur1 = new Survey();
    Survey sur2= new Survey();
    Survey sur3 = new Survey();
    sur1.setScale(scale);
    sur1.setTotal(0);
    sur1.setResult(1L);
    sur2.setScale(scale);
    sur2.setTotal(0);
    sur2.setResult(1L);
    sur3.setScale(scale);
    sur3.setTotal(0);
    sur3.setResult(1L);
    
    if(count!=0){
      for(Survey s:list){
        
        if(s.getResult()==1){
          float sc= (float)s.getTotal()/(float)count*100;
          DecimalFormat df2 = new DecimalFormat("#"); 
          sur1.setScale(df2.format(sc)+"%");
          sur1.setResult(s.getResult());
          sur1.setTotal(s.getTotal());
        }else if(s.getResult()==2){
          float sc= (float)s.getTotal()/(float)count*100;
          DecimalFormat df2 = new DecimalFormat("#"); 
          sur2.setScale(df2.format(sc)+"%");
          sur2.setResult(s.getResult());
          sur2.setTotal(s.getTotal());
        }else if(s.getResult()==3){
          float sc= (float)s.getTotal()/(float)count*100;
          DecimalFormat df2 = new DecimalFormat("#"); 
          sur3.setScale(df2.format(sc)+"%");
          sur3.setResult(s.getResult());
          sur3.setTotal(s.getTotal());
        }
       
      }
    }
    List<Survey> list2 = new ArrayList<Survey>();
    
    list2.add(sur1);
    list2.add(sur2);
    list2.add(sur3);
    
    mv.addObject("list", list2);
     
    return mv;
  }
  
  /**
   * 初始化
   * @return
   */
  @RequestMapping("/list")
  public ModelAndView list(@RequestParam(required = false) Integer s) {
    ModelAndView mv = new ModelAndView();
    if(s!=null)
      switch(s) {
        case 1: mv.addObject("msg", "保存成功！");break;
        case 2: mv.addObject("msg", "更新成功！");break;
        case 3: mv.addObject("msg", "删除成功！");break;
        case 4: mv.addObject("msg", "操作失败！");break;
        default: break;
      }
    mv.setViewName("/operate/surveyList.jsp");
    return mv;
  }
  
  /**
   * 异步查询
   * @param helpInfo
   * @return
   */
  @RequestMapping("/queryByPage")
  @ResponseBody
  public Map<String, Object> queryByPage(Survey helpInfo) {
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    try {
      jsonMap.put("dataList", surveyService.query(helpInfo));
      jsonMap.put("iTotalDisplayRecords", surveyService.count(helpInfo));
      jsonMap.put("iTotalRecords", surveyService.count());
      System.out.println(surveyService.count());
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询帮助中心问题异常", e);
    }
    return jsonMap;
  }
  
  /**
   * 编辑
   * @param id
   * @return
   */
  @RequestMapping("/edit/{id}")
  public ModelAndView edit(@PathVariable Long id) {
    ModelAndView mv = new ModelAndView("/operate/surveyDetail.jsp");
    Survey survey = new Survey();
    survey.setCreateTime(DateUtil.getCurrentDate2());
    mv.addObject("data", id==null||id==0 ? survey : this.surveyService.find(id));
    return mv;
  }
  /**
   * 新增
   * @return
   */
  @RequestMapping("/edit")
  public ModelAndView edit() {
    return edit(null);
  }
  
  /**
   * 删除帮助中心
   * @return
   */
  @RequestMapping("/del/{id}")
  public ModelAndView del(@PathVariable Long id) {
    ModelAndView mv = new ModelAndView();
    try {
      this.surveyService.delete(id);
      mv.setViewName(this.viewName.concat("3"));
    } catch(Exception e) {
      mv.setViewName(this.viewName.concat("4"));
      BaseLog.e(getClass(), "删除投票异常！", e);
    }
    return mv;
  }


  
}
